package com.dbManage.page.note;

import com.dbManage.common.Constant;
import com.dbManage.dao.AuthDao;
import com.dbManage.dao.NoteDao;
import com.dbManage.entity.AuthRecord;
import com.dbManage.entity.Note;
import com.dbManage.entity.User;
import com.dbManage.exception.CommonException;
import com.dbManage.tools.SessionTool;
import com.wsse.common.tool.TimeTool;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class NoteService {
    @Resource
    NoteDao noteDao;

    @Resource
    AuthDao authDao;

    public void addNote(Note note) throws CommonException {
        Note temp=noteDao.findNoteByUrl(note.getUrl());
        if (temp!=null)throw new CommonException("链接已存在!");
        User user= SessionTool.getUser();
        note.setAddId(user.getId());
        note.setAddName(user.getNickName());
        note.setTime(TimeTool.getNow());
        noteDao.insertNote(note);
    }

    public void updateNote(Note note){
        noteDao.updateNote(note);
    }

    public List<Note> queryNote(Note note) {
        if(note==null){
            note=new Note();
            note.setStatus(-1);
        }
        if (SessionTool.getUser().getRole()!= Constant.ROLE_SUPER_MANAGER)
            note.setAddId(SessionTool.getUser().getId());
        else
            note.setAddId(0);
        return noteDao.queryNote(note);
    }

    public void insertAuth(Note note){
        User user= SessionTool.getUser();
        AuthRecord auth = new AuthRecord();
        auth.setNid(note.getId());
        auth.setStatus(note.getStatus());
        auth.setHandleId(user.getId());
        auth.setHandleName(user.getNickName());
        auth.setTime(TimeTool.getNow());
        authDao.insertAuth(auth);
    }


}
